//: Playground - noun: a place where people can play

import UIKit

/**
*  for循环
*/

//1.for-in循环

for i in 1...5 {
    
    print("\(i)")
    
}

//如果你不需要序列中的每一个值，可以使用_来忽略它，仅仅只是使用循环体本身：

let base = 3
let power = 10
var answer = 1

for _ in 1...power {
    
    answer *= base
    
}
print("\(base) to the power of \(power) is \(answer)")
let names = ["Anna","1","Bob","2"]

var number  = "Bob"
for name in names{
    
    if name.isEqual(number){
        print(name)
    }
    if (Int(name) != nil){
        
        print(name)
    }
}

while 1 > 0{
    
    /**
    *  代码
    */
    
}

//repeat-while循环
repeat {
    
   //print("1")
    
} while 0 < 1


/// switch
var i = 0
switch i {
    
case 0:
    "0"
case 1:
    "1"
default:
    "2"
    
}
/// Switch能同时执行两条case
let k = 3
switch k{
case 0,3:
    "0"
case 1:
    "1"
default:
    "2"
}
//想执行完一条case之后，想继续执行下一条case
var n = 0
switch n {
case 0:
    "0"
    fallthrough
case 1:
    "1"
case 2:
    "2"
default:
    "default"
}
/**
*  支持多种数据类型
*/
//浮点型
let float = 1.5
switch float{
case 1.5:
    "1.5"
default:
    "default"
    
}
//支持布尔
let isSuccess = true
switch isSuccess {
case true:
    "true"   //被输出
default:
    "default"
}
//字符串
let name = "Bannings"
switch name {
case "Bannings":
    "Bannings"   //被输出
default:
    "default"
}
//区间
var m = 15
switch m {
case 0 ... 10:
    "0~10"
case 11 ... 20:
    "11~20" //被输出
default:
    "default"
}
//元组
let request = (true,"success")
switch request {
case (true, "success"):
    "success"   //被输出
case (false, "failed"):
    "failed"
default:
    "default"
}
//不关心的值直接 ，下划线忽略
switch request {
case (_, "success"):
    "success"   //被输出
case (false, _):
    "failed"
default:
    "default"
}
let requests = (10,"failed")
switch requests {
case (_, "success"):
    "success"
case (1 ... 10, _):
    "1~10"    //被输出
default:
    "default"
}
//Value Binding 能把元组中的各个值提取出来，然后直接在下面使用 不能使用fallthrought
switch request {
case (true, let state):
    state    //被输出:success
case (let errorCode, _):
    "error code is \(errorCode)"
}
let requestss = (0,"success")
switch requestss {
case let (errorCode, state):
    state    //被输出:success
case (let errorCode, _):
    "error code is \(errorCode)"
}

//支持额外的逻辑判断
//switch语句可以使用where关键词来增加判断的条件
switch requestss {
    
case (0, let state) where state != "success":
    state
case (let errorCode, _):
    "error code is \(errorCode)"
}

//控制跳转语句
//continue、break、fallthrough、return

//
let puzzleInput = "great minds think alike"
var puzzleOutput = ""
for character in puzzleInput.characters {
    switch character {
    case "a", "e", "i", "o", "u", " ":
        continue
    default:
        puzzleOutput.append(character)
    }
}
print(puzzleOutput)

//break语句将终止整个循环的执行，可以用在循环语句中，也可以用在switch语句中。

let numberSymbol: Character = "三"  // Simplified Chinese for the number 3
var possibleIntegerValue: Int?
switch numberSymbol {
case "1", "١", "一", "๑":
    possibleIntegerValue = 1
case "2", "٢", "二", "๒":
    possibleIntegerValue = 2
case "3", "٣", "三", "๓":
    possibleIntegerValue = 3
case "4", "٤", "四", "๔":
    possibleIntegerValue = 4
default:
    break
}
if let integerValue = possibleIntegerValue {
    print("The integer value of \(numberSymbol) is \(integerValue).")
} else {
    print("An integer value could not be found for \(numberSymbol).")
}
let finalSquare = 25
var board = [Int](count: finalSquare + 1, repeatedValue: 0)
board[03] = +08; board[06] = +11; board[09] = +09; board[10] = +02
board[14] = -10; board[19] = -11; board[22] = -02; board[24] = -08
var square = 0
var diceRoll = 0

gameLoop: while square != finalSquare {
    if ++diceRoll == 7 { diceRoll = 1 }
    switch square + diceRoll {
    case finalSquare:
        // diceRoll will move us to the final square, so the game is over
        break gameLoop
    case let newSquare where newSquare > finalSquare:
        // diceRoll will move us beyond the final square, so roll again
        continue gameLoop
    default:
        // this is a valid move, so find out its effect
        square += diceRoll
        square += board[square]
    }
}
print("Game over!")

